package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddAdvimgDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.IYddAdvimgService;
import com.itheima.ydd.service.IYddAdvposService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_advimg")
public class YddAdvimgController {
    @Autowired
    private IYddAdvimgService iYddAdvimgService;
    @Autowired
    private IYddAdvposService iYddAdvposService;

    /**
     * 广告图分页查询
     *
     * @param advimgdesc
     * @param advimgpos
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
//    @Cacheable(value = "yddadvimg", key = "#p0 +'_'+ #p1 + '_' + #p2 + '_' + #p3")
    public R list(String advimgdesc, Long advimgpos, Integer page, Integer limit) {
        // 根据条件筛选广告图
        Page<YddAdvimg> pageInfo = new Page<>(page, limit);
        LambdaQueryWrapper<YddAdvimg> lqwYddAdvimg = new LambdaQueryWrapper<>();
        lqwYddAdvimg.like(advimgdesc != null, YddAdvimg::getAdvimgdesc, advimgdesc)
                .eq(advimgpos != null, YddAdvimg::getAdvimgpos, advimgpos);
        iYddAdvimgService.page(pageInfo, lqwYddAdvimg);
        // 向dto中注入广告位的名称
        List<YddAdvimgDto> yddAdvimgDtos = new ArrayList<>();
        for (YddAdvimg yddAdvimg : pageInfo.getRecords()) {
            YddAdvimgDto yddAdvimgDto = new YddAdvimgDto();
            BeanUtils.copyProperties(yddAdvimg, yddAdvimgDto);
            // 根据id查找广告位
            YddAdvpos byId = iYddAdvposService.getById(yddAdvimg.getAdvimgpos());
            yddAdvimgDto.setAdvposname(byId.getAdvposname());
            yddAdvimgDtos.add(yddAdvimgDto);
        }
        // 设置返回值map
        Map map = new HashMap();
        map.put("count", (int) pageInfo.getTotal());
        map.put("totalPages", (int) (pageInfo.getTotal() / pageInfo.getSize() + 1));
        map.put("pageSize", (int) pageInfo.getSize());
        map.put("currentPage", (int) pageInfo.getCurrent());
        map.put("data", yddAdvimgDtos);
        return new R(map, "", 0);
    }

    /**
     * 添加广告图
     *
     * @param yddAdvimg
     * @return
     */
    @PostMapping("/add")
//    @CacheEvict(value = "yddadvimg", allEntries = true)
    public ResultData add(@RequestBody YddAdvimg yddAdvimg) {
        iYddAdvimgService.save(yddAdvimg);
        return ResultData.ok("", "");
    }

    /**
     * 根据id查询广告图
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
//    @Cacheable(value = "yddadvimg", key = "#p0")
    public ResultData rowInfo(Long id) {
        YddAdvimg byId = iYddAdvimgService.getById(id);
        return ResultData.ok("", byId);
    }

    /**
     * 修改广告图
     *
     * @param yddAdvimg
     * @return
     */
    @PostMapping("/update")
//    @CacheEvict(value = "yddadvimg", allEntries = true)
    public ResultData update(@RequestBody YddAdvimg yddAdvimg) {
        iYddAdvimgService.updateById(yddAdvimg);
        return ResultData.ok("", "");
    }

    /**
     * 删除广告图
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
//    @CacheEvict(value = "yddadvimg", allEntries = true)
    public ResultData del(Long id) {
        System.err.println(id);
        iYddAdvimgService.removeById(id);
        return ResultData.ok("", "");
    }

}
